package com.bjpowernode.controller;

import com.bjpowernode.util.LoginInfoUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;

//@RestController
@Controller
public class UserController {
    @ResponseBody
    @RequestMapping("/")
    public String index(){
        return "Welcome to Spring Security";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @ResponseBody   //通过Principal可以拿到用户的登录信息，也就是UserDetails相关信息
    @RequestMapping("/welcome")
    public Object welcome(Principal principal){
        return principal;
    }

    @ResponseBody   //通过Authentication也可以拿到用户信息，它是Principal的子接口
    @RequestMapping("/welcome2")
    public Object welcome2(Authentication authentication){
        return authentication;
    }

    //通过UsernamePasswordAuthenticationToken也可以拿到用户信息，他是Authentication的实现类
    @ResponseBody
    @RequestMapping("/welcome3")
    public Object welcome3(UsernamePasswordAuthenticationToken upat){
        return upat;
    }

    @ResponseBody
    @RequestMapping("/welcome4")
    public Object welcome4(){
        //也可以通过这个方法获取用户登录信息
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @ResponseBody
    @RequestMapping("/welcome5")
    public Object welcome5(){
        //自己写的工具类获取当前登录用户
        return LoginInfoUtil.getCurrentLoginUser();
    }
}
